package Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import DBOperation.DBOperation;

/**
 * Servlet implementation class userLogin
 */
@WebServlet("/userLogin")
public class UserLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
//	public static String staticName = "";
//	public static String staticPassword = "";
	
	public static HttpSession session;
	
	public static Map sessionMap = new HashMap();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserLoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    
    public void init(ServletConfig config) throws ServletException
    {
    	super.init(config);
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//System.out.println( staticName + "    " +staticPassword);
				
		Enumeration parameters = request.getParameterNames();
		String username = "";
		String password = "";
		
		while(parameters.hasMoreElements())
		{
			String str = parameters.nextElement().toString();
			
			if(str.equals("username"))
				username = request.getParameter(str);
			if(str.equals("password"))
				password = request.getParameter(str);
		}
		
		//System.out.println(username + "   " + password);
		
		DBOperation dbo = new DBOperation();
		ResultSet rs = dbo.userLogin(username, password);
		
		//创建用户session
		try {
			if(rs.next())
			{
				session = request.getSession(true);
				if(session != null)
				{
					session.setAttribute("userid", rs.getInt("id"));
					session.setAttribute("username", username);
					session.setAttribute("company", rs.getString("company"));
					System.out.println("===="+session.getAttribute("userid")+"====");
					System.out.println("===="+session.getAttribute("username")+"====");
					System.out.println("===="+session.getAttribute("company")+"====");
				}
				response.sendRedirect("./home.jsp");
				return;
			}
			else
			{
				PrintWriter out = response.getWriter();
				out.println("<script>alert('用户名或密码错误！'); history.go(-1);</script>");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		/**
		Map map = new HashMap();
		map.put("username", username);
		map.put("password", password);
		
		int sessionId = (int)(Math.random()*10000);
		System.out.println("####SessionId: " + sessionId + "####");
		sessionMap.put(sessionId, map);
		
		testSessionMap(sessionMap);
		**/
	}
	
	protected void testSessionMap(Map map)
	{
		if(map.isEmpty())
			System.out.println("----SessionMap is Empty----");
		else
		{
			Set<Integer> key = map.keySet();
			
			Iterator it = key.iterator(); 
			
			while(it.hasNext())
			{
				
				int sessionId = (Integer)it.next();
				
				String testStr = "sessionId: " + sessionId + "; ";
				
				Map innerMap = (Map)map.get(sessionId);
				
				Set<String> innerKey = innerMap.keySet();
				
				Iterator innerIt = innerKey.iterator();
				
				while(innerIt.hasNext())
				{
					String sessionKey = (String)innerIt.next();
					
					testStr += sessionKey +": ";
					
					String sessionValue = (String)innerMap.get(sessionKey);
					
					testStr += sessionValue + "; ";
					
				}
				
				System.out.println(testStr);
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
